package com.cuc.platform.core.componet.security.dao.jdbc;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cuc.platform.core.componet.security.dao.SecurityUserDao;
import com.cuc.platform.core.componet.security.domain.SecurityUser;

/**
 * @author peepul
 *
 * 
 */
@Repository("SecurityUserDao")
public class SecurityUserDaoImpl  implements SecurityUserDao {

	public SecurityUserDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<SecurityUser> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SecurityUser getUserById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SecurityUser getUserByLoginName(String userName) {
		final String sql = "select ss.*,sui.org_id from ss_user ss,sa_userinfo sui where ss.userinfo_id = sui.user_info_id  and ss.loginName = ? ";

		return jdbcTemplate.queryForObject(sql, new String[]{userName}, new BeanPropertyRowMapper<SecurityUser>(SecurityUser.class));
	}

	@Override
	public List<Map<String, Object>> getUserWithRoles(String userName) {
/*	 	select 
 		u.id , u.aliasName, u.loginName , u.password, u.salt, u.status, u.isadmin, u.descn, u.userinfo_id,  
 		r.id as r_id, r.name as r_name, r.isadmin as r_isadmin, r.descn as r_descn
	from ss_user u
			left join ss_user_role ur on u.id = ur.user_id
			left join ss_role r on r.id = ur.role_id
		where u.username = #{userName}*/
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("select");
		sb.append(" u.id, u.aliasName, u.loginName , u.password, u.salt, u.status, u.isadmin, u.descn, u.userinfo_id, ");
		sb.append(" r.id as r_id, r.name as r_name, r.isadmin as r_isadmin, r.descn as r_descn ");
		sb.append(" from ss_user u ");
		sb.append(" left join ss_user_role ur on u.id = ur.user_id ");
		sb.append(" left join ss_role r on r.id = ur.role_id ");
		sb.append(" where u.loginname = ?");
		
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString(),  new Object[]{userName});
		
		return list;
	}

	@Override
	public void insertUser(SecurityUser user) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUser(SecurityUser user) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public int delUserById(Long id) throws DataAccessException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateUserStatus(SecurityUser user) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public List<SecurityUser> viewRoleswithUsers(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delUserRoleUnions(Long userId) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public void saveUserRoleUnions(Map<String, Long> map) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * Ioc
	 */
	@Resource
	private JdbcTemplate jdbcTemplate;

}
